﻿using LD.Host;
using System;

namespace LD.Tool
{
    public partial class VmCalibFrm : FrmToolBase
    {
        public VmCalibFrm()
        {
            InitializeComponent();
            this.Shown += ToolMesSharpFrm_Shown;
            this.btn_ok.Click += Btn_ok_Click;
        }

        private void ToolMesSharpFrm_Shown(object sender, EventArgs e)
        {
            Init();
        }

        private void Init()
        {
            if (TB_Tool.Param != null)
            {
                var param = Newtonsoft.Json.JsonConvert.DeserializeObject<VmCalibParam>(TB_Tool.Param);
                if (param != null)
                {
                    this.txt_offset.Text = param.Offset + "";
                    this.txt_plcRepPic.Text = param.RepPictureAddress;
                    this.txt_plcResPic.Text = param.ResPictureAddress;
                    this.txt_repLocal.Text = param.RepPointAddress;
                    this.txt_resLocal.Text = param.ResPointAddress;
                    this.txt_offsetX.Text = param.PlcOffset_X;
                    this.txt_offsetY.Text = param.PlcOffset_Y;
                    this.txt_ccdGlobalX.Text = param.CCD_X;
                    this.txt_ccdGlobalY.Text = param.CCD_Y;
                    this.num_waitVal.Value = param.WaitTime;
                    this.cob_camera.Text = param.FlowName;
                    this.txt_currentX.Text = param.Current_X;
                    this.txt_currentY.Text = param.Current_Y;
                }
            }
        }

        private void Btn_ok_Click(object sender, EventArgs e)
        {
            if (int.TryParse(this.txt_offset.Text, out var offset))
            {
                var param = new VmCalibParam()
                {
                    Offset = offset,
                    RepPictureAddress = this.txt_plcRepPic.Text,
                    ResPictureAddress = this.txt_plcResPic.Text,
                    RepPointAddress = this.txt_repLocal.Text,
                    ResPointAddress = this.txt_resLocal.Text,
                    PlcOffset_X = this.txt_offsetX.Text,
                    PlcOffset_Y = this.txt_offsetY.Text,
                    CCD_X = this.txt_ccdGlobalX.Text,
                    CCD_Y = this.txt_ccdGlobalY.Text,
                    WaitTime = (int)this.num_waitVal.Value,
                    FlowName = this.cob_camera.Text,
                    Current_X = this.txt_currentX.Text,
                    Current_Y = this.txt_currentY.Text
                };
                TB_Tool.Param = Newtonsoft.Json.JsonConvert.SerializeObject(param);

            }
        }
    }
}
